#include <filetransffermgr.h>

bool FileTransfferMgr::appendFileStruct(std::shared_ptr<FileTransfferStruct> pFileStruct)
{
    openfiles_[pFileStruct->getSessionId()][pFileStruct->getFileName()] = pFileStruct;
    return true;
}

bool FileTransfferMgr::existFileStruct(const std::string& sessionId, const std::string& filename)
{
    if (openfiles_.find(sessionId) == openfiles_.end()) {
        return false;
    }
    if (openfiles_[sessionId].find(filename) == openfiles_[sessionId].end()) {
        return false;
    }
    return true;
}

std::shared_ptr<FileTransfferStruct> FileTransfferMgr::getFileStruct(const std::string& sessionId, const std::string& filename)
{
    if (!existFileStruct(sessionId, filename)) {
        return nullptr;
    }
    return openfiles_[sessionId][filename];
}

bool FileTransfferMgr::removeFileStruct(const std::string& sessionId, const std::string& fileName)
{
    if (!existFileStruct(sessionId, fileName)) {
        return true;
    }
    openfiles_[sessionId].erase(fileName);
    if (openfiles_[sessionId].size() == 0) {
        openfiles_.erase(sessionId);
    }
    return true;
}

bool FileTransfferMgr::removeFileStruct(const std::string& sessionId)
{
    openfiles_.erase(sessionId);
    return true;
}
